package me.firstapp.bbs.controller;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

import me.firstapp.bbs.base.BaseController;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.ResponseUtils;

@Controller
public class UploadController extends BaseController {

	@Value("#{configProperties['qiniu.upload.accessKey']}")
	private String accessKey;

	@Value("#{configProperties['qiniu.upload.secret']}")
	private String secret;

	@Value("#{configProperties['qiniu.upload.bucketname']}")
	private String bucketName;

	@Value("#{configProperties['qiniu.upload.domain']}")
	private String domain;

	@PostMapping("/upload")
	@ResponseBody
	public void upload(@RequestParam("file") MultipartFile file, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		getCurrentLoginMemberId(request, response);
		try {
			if (file == null) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "文件");
			}
			if ((file.getSize() / 1024) > 2048) {
				throw new ServiceException(StatusHouse.COMMON_FILE_MAX_TWO);
			}
			System.out.println("开始上传文件......");
			Zone z = Zone.autoZone();
			Configuration c = new Configuration(z);
			UploadManager uploadManager = new UploadManager(c);
			String uploadToken = Auth.create(accessKey, secret).uploadToken(bucketName);
			// 调用put方法上传
			String key = UUID.randomUUID().toString().replaceAll("-", "");
			Response res = uploadManager.put(file.getBytes(), key, uploadToken);
			// 打印返回的信息
			System.out.println("结束上传文件，返回码:" + res.statusCode + "\n錯誤信息：" + res.error + "\n返回信息:" + res.bodyString());
			if (res.statusCode == 200) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(domain + "/" + key);
			}
			ResponseUtils.renderJson(response, result);
		} catch (QiniuException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@RequestMapping(value = "/getUploadToken.do", method = RequestMethod.GET)
	public void getUploadToken(HttpServletRequest request, HttpServletResponse response) {
		getCurrentLoginMemberId(request, response);
		SingleObject<String> result = new SingleObject<>();
		try {
			String uploadToken = Auth.create(accessKey, secret).uploadToken(bucketName);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(uploadToken);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
